﻿using System.Net;
using System.Web.Mvc;
using Hrm.Core;
using Hrm.Services;

namespace Hrm.Web.Areas.Studio.Controllers
{
    public class BlogPostsController : Controller
    {
        private readonly IBlogService _blogService;

        public BlogPostsController(IBlogService blogService)
        {
            this._blogService = blogService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult PageBlogs(PageRequest<BlogPost> request)
        {
            var data = this._blogService.PageBlogPostList(request);
            return this.JsonNet(data);
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BlogPost blogPost = this._blogService.GetById(id);
            if (blogPost == null)
            {
                return HttpNotFound();
            }
            return View(blogPost);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Include = "Id,Title,Description,Type,Content,Tags")] BlogPost blogPost)
        {
            if (ModelState.IsValid)
            {
                this._blogService.Insert(blogPost);
                return RedirectToAction("Index");
            }

            return View(blogPost);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BlogPost blogPost = this._blogService.GetById(id);
            if (blogPost == null)
            {
                return HttpNotFound();
            }
            return View(blogPost);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(BlogPost blogPost)
        {
            if (ModelState.IsValid)
            {
                BlogPost model = this._blogService.GetById(blogPost.Id);
                if (TryUpdateModel(model, "", new[] { "Id", "Title", "Description", "Type", "Content", "Tags" }))
                    this._blogService.Update(model);
                return RedirectToAction("Index");
            }
            return View(blogPost);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            BlogPost model = this._blogService.GetById(id);
            this._blogService.UpdateDisabled(model);
            return RedirectToAction("Index");
        }

        public ActionResult CommentManager(int id)
        {
            BlogPost model = this._blogService.GetById(id);
            return View(model.BlogComments);
        }

        public JsonResult CommentDelete(int id)
        {
            ResponseModel result = new ResponseModel();
            if(!this._blogService.DeleteComment(id))
            {
                result.SetFailed(Constant.OperateFail);
            }
            return Json(result);
        }
    }
}
